package com.example.runningman.view;

import android.content.Context;
import android.content.res.TypedArray;
import android.util.AttributeSet;
import android.view.LayoutInflater;
import android.view.View;
import android.widget.ImageView;
import android.widget.LinearLayout;
import android.widget.TextView;

import com.example.runningman.R;

import androidx.annotation.Nullable;

public class ItemView extends LinearLayout {
    private ImageView imageView;//item的图标
    private TextView textView;//item的文字
    private ImageView imageView_right;
    private Integer a;
    private ImageView bottomview;
    private boolean isbootom=true;//是否显示底部的下划线
    private boolean isRight=true;
    public ItemView(Context context) {
        this(context,null);
    }
    public ItemView(Context context, @Nullable AttributeSet attrs) {
        this(context, attrs,-1);
    }
    public ItemView(Context context, @Nullable AttributeSet attrs, int defStyleAttr) {
        super(context, attrs, defStyleAttr);

        //加载布局
        LayoutInflater.from(getContext()).inflate(R.layout.item_view,this);
        //获取设置属性对象
        TypedArray ta=context.obtainStyledAttributes(attrs,R.styleable.ItemView);
        //获取控件，设置属性值
        isbootom=ta.getBoolean(R.styleable.ItemView_show_bottomline,true);
        isRight=ta.getBoolean(R.styleable.ItemView_show_rightimg, true);
        bottomview=findViewById(R.id.item_bottom);
        imageView=findViewById(R.id.item_img);
        textView=findViewById(R.id.item_text);
        imageView_right=findViewById(R.id.item_iright);
        textView.setText(ta.getString(R.styleable.ItemView_show_text));
        a=ta.getResourceId(R.styleable.ItemView_show_leftimg,1);
        imageView.setBackgroundResource(ta.getResourceId(R.styleable.ItemView_show_leftimg,R.drawable.setting));
        //回收属性对象
        ta.recycle();
        initview();
    }
    private void initview(){
        //如果isbootom为true，显示下划线，否则隐藏
        if(isbootom){
            bottomview.setVisibility(View.VISIBLE);
        }else{
            bottomview.setVisibility(View.GONE);
        }
        if(isRight){
            imageView_right.setVisibility(View.VISIBLE);
        }else {
            imageView_right.setVisibility(View.GONE);
        }
        if(a==1){
            imageView.setVisibility(View.GONE);
        }
    }


}